package create_module

import (
	"errors"
	"fmt"
	"math/rand"
	"time"
)

// := 的含义即声明并赋值
/**
即
var message string
message = fmt.Sprintf...
省略了var声明
*/

// Hello 格式化name返回新字符串 %v占位符
func /*方法名*/ Hello(name string /*参数类型*/) string /*返回类型*/ {
	message := fmt.Sprintf(randomFormat(), name)
	return message
}

func /*方法名*/ Hello2(name string /*参数类型*/) (string, error) /*返回类型*/ {
	if name == "" {
		return "", errors.New("This is error msg ! may not empty name in this func")
	}
	message := fmt.Sprintf(randomFormat(), name)
	// make error
	//message := fmt.Sprintf(randomFormat())
	return message, nil
}

func Hello_ok(name string) (string, error) {
	if name == "" {
		return "", errors.New("This is error msg ! may not empty name in this func")
	}
	//message := fmt.Sprintf(randomFormat(), name)
	// make error
	message := fmt.Sprintf(randomFormat(), name)
	return message, nil
}

func Hellos(names []string) (map[string]string, error) {
	messages := make(map[string]string)
	for _, name := range names {
		message, err := Hello2(name)
		if err != nil {
			return nil, err
		}
		messages[name] = message
	}
	return messages, nil
}

// init sets initial values for variables used in the function
func init() {
	rand.Seed(time.Now().UnixNano())
}

func randomFormat() string {
	formats := []string{
		"Hi, %v, Welcome!",
		"Great to see you , %v",
		"Hail, %v! Well met!",
	}

	return formats[rand.Intn(len(formats))]
}
